/*
 * http://gr0w.com/
 * Copyright (c) 2006 Jon Tan and Paul Whitrow
 * Other functions based on those by:Jeremy Keith http://adactio.com/
 */

/*
 * Generic onload event function by Simon Willison
 */

function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') window.onload = func;
    else {
        window.onload = function(){
            if (oldonload) oldonload();
            func();
        }
    }
}

function createElement(element) {
	element = element.toLowerCase();
	if (typeof document.createElementNS != 'undefined')
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	if (typeof document.createElement != 'undefined')
		return document.createElement(element);
	return false;
}

function stopDefaultEvent(e){
	if (window.event){
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (e && e.preventDefault && e.stopPropagation){
		e.preventDefault();
		e.stopPropagation();
	}
}

// test to see if function and objects are available before allowing the function to continue
function objTest(){
	if(!document.getElementById) return false;
	var result=true;
	var len=arguments.length;
	for(var a=0;a<len;a++){
		if(!document.getElementById(arguments[a])){
			if(result) result=!result;
		}
	}
	return result;
}

/*
 * Clear form fields default value onblur
 */

/*
function resetFields(whichform){
	var len=whichform.elements.length;
	for(var i=0;i<len;i++){
		var element = whichform.elements[i];
		if((element.type)&&(element.type == "submit")) continue;
		if((element.type)&&(element.type == "reset")) continue;
		if((element.type)&&(element.type == "radio")) continue;
		if((element.type)&&(element.type == "checkbox")) continue;
		if((element.type)&&(element.type == "textarea")) continue;
		if(!element.defaultValue) continue;
		element.onfocus = function(){
			if(this.value == this.defaultValue) this.value = "";
		}
		element.onblur = function(){
			if(this.value == "") this.value = this.defaultValue;
		}
	}
}

function prepareForms(){
	var len=document.forms.length;
	for(var i=0;i<len;i++){
		var thisform = document.forms[i];
		resetFields(thisform);
	}
}

addLoadEvent(prepareForms);
*/

/*
 * Archive date tabs
 */


function initTabs(action,obj){
	if(!document.getElementById || !document.getElementsByTagName) return;
	var tabs=document.getElementById("tabs");
	if(!tabs) return;
	var togLi=tabs.getElementsByTagName("li");
	var numLi=togLi.length;
	if(numLi<1) return;
	var id;
	for(var i=0;i<numLi;i++){
		var toggle = togLi[i].getElementsByTagName("a")[0];

		// hide tab contents except current year
		id=toggle.hash.substr(1);
		if(!togLi[i].className.match('live')) document.getElementById(id).style.display='none';

		// add onclick
		toggle.onclick=function(e){
			showTab(this.hash.substr(1));
			if(typeof e == "undefined") e=window.event;
			stopDefaultEvent(e);
		}
		if(toggle.captureEvents) toggle.captureEvents(Event.CLICK);
	}
}

function showTab(objID){
	var tabs=document.getElementById("tabs");
	var toggles=tabs.getElementsByTagName("a");
	var len=toggles.length;
	for(var i=0;i<len;i++){
		// hide tab contents except selected year
		id=toggles[i].hash.substr(1);
		document.getElementById(id).style.display=(id != objID) ? 'none' : 'block';
		toggles[i].parentNode.className=(id != objID) ? '' : 'live';
	};
}

addLoadEvent(initTabs);

/*
 * Show or hide comments style guide
 */

function initStyleGuide(){
	if(!document.getElementById) return;
	var guide=document.getElementById("style-guide");
	if(!guide) return;

	var controlsId='style-guide-controls';

	var guideControls=createElement("div");
	guideControls.id=controlsId;

	var pOff=createElement("p");
	var aOff=createElement("a");
	aOff.href='javascript:;';
	aOff.onclick = function(){
		toggleStyleGuide(this.className);
	}
	if (aOff.captureEvents) aOff.captureEvents(Event.CLICK);
	aOff.className='toggledown';
	aOff.title='Show Style Guide';
	aOff.appendChild(document.createTextNode('See the Style Guide'));
	pOff.style.display='block';
	pOff.appendChild(aOff);
	guideControls.appendChild(pOff);

	var pOn=createElement("p");
	var aOn=createElement("a");
	aOn.href='javascript:;';
	aOn.onclick = function(){
		toggleStyleGuide(this.className);
	}
	if(aOn.captureEvents) aOn.captureEvents(Event.CLICK);
	aOn.className='toggleup';
	aOn.title='Hide Style Guide';
	aOn.appendChild(document.createTextNode('Hide the Style Guide'));
	pOn.style.display='none';
	pOn.appendChild(aOn);
	guideControls.appendChild(pOn);

	guide.style.display='none';
	guide.parentNode.insertBefore(guideControls,guide);
}

function toggleStyleGuide(objClass){
	var controlsId='style-guide-controls';
	var controlsDiv=document.getElementById(controlsId);
	var controlsP=controlsDiv.getElementsByTagName("p");
	var len=controlsP.length;
	for(var i=0;i<len;i++){
		controlsP[i].style.display = (controlsP[i].style.display=='none') ? 'block' : 'none';
	}
	document.getElementById("style-guide").style.display=(objClass=='toggleup') ? 'none' : 'block';
}

addLoadEvent(initStyleGuide);

/*
 * Show or hide account-prompt
 */

function accountprompt(action){
    if(!objTest('account-prompt-on','account-prompt-off')) return;
    if (action == 'on'){
        var obj1 = document.getElementById('account-prompt-on');
        var obj2 = document.getElementById('account-prompt-off');
    } else {
        var obj1 = document.getElementById('account-prompt-off');
        var obj2 = document.getElementById('account-prompt-on');
    }
    if (obj1) obj1.style.display = 'block';
    if (obj2) obj2.style.display = 'none';

}

addLoadEvent(accountprompt);

/*
 * Show or hide password field if OpenID chosen
 */

function hidepass(obj){
	var obj1=document.getElementById('openidlink');
	var obj2=document.getElementById('pass1');
	var obj3=document.getElementById('pass2');
	var obj4=document.getElementById('pass-current');
	if (obj.checked){
		if(obj1) obj1.style.display = 'none';
		if(obj2) obj2.style.display = 'none';
		if(obj3) obj3.style.display = 'none';
		if(obj4) obj4.style.display = 'none';
	}else{
		if(obj1) obj1.style.display = 'block';
		if(obj2) obj2.style.display = 'block';
		if(obj3) obj3.style.display = 'block';
		if(obj4) obj4.style.display = 'block';
	}
}
